#!/bin/ash
#
# CXL WITH SYSTEM RAM

# DETECT CXL TOPOLOGY
CMDLINE=$(cat /proc/cmdline)
CXL_TOPOLOGY=$(echo "${CMDLINE}" | grep -o 'BiscuitOS_CXL_TPG_[^ ]*')

# PREPARE ENVIRONMENT
echo 150 > /proc/sys/vm/nr_hugepages

# INSTALL SPECIAL MODULE
insmod /lib/modules/$(uname -r)/kernel/drivers/dax/kmem.ko
if ! lsmod | grep -q "^kmem"; then
	echo "'kmem' MODULE DOESN'T INSTALL, PLS EXEC"
	echo "=> cd */package/BiscuitOS-kernel-default/"
	echo "=> make module ; make module_install ; make pack"
	echo "THEN, RETRY"
	exit 0
fi

# BUILD SPECIFY DEVDAX
if [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_0" ]; then
	echo "BiscuitOS CXL TYPE3: x0 Switch and x1 PMEM"

	# CXL SETUP
	cxl destroy-region -f all
	cxl create-region -d decoder0.0 -m mem0 -s 512M -t pmem -w 1 -g 4096 -u
	cxl enable-region region0

	# NDCTL LIST
	ndctl destroy-namespace --force all
	ndctl enable-region region0
	# BUILD DEVDAX ON /dev/dax0.0
	ndctl create-namespace --mode=dax -f

	# RECONFIG TO SYSTEM RAM
	daxctl reconfigure-device  -N --mode=system-ram dax0.0 --force

elif [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_1" ]; then
	echo "BiscuitOS CXL TYPE3: x0 Switch and x1 DDR"

	# CXL SETUP
	cxl destroy-region -f all
	cxl create-region -d decoder0.0 -m mem0 -s 512M -t ram -w 1 -g 4096 -u
	cxl enable-region region0

	# RECONFIG TO SYSTEM RAM
	daxctl reconfigure-device  -N --mode=system-ram dax0.0 --force

elif [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_2" ]; then
	echo "BiscuitOS CXL TYPE3: x1 Switch and x1 PMEM"

	# CXL SETUP
	cxl destroy-region -f all
	cxl create-region -d decoder0.0 -m mem0 -s 512M -t pmem -w 1 -g 4096 -u
	cxl enable-region region0

	# NDCTL LIST
	ndctl destroy-namespace --force all
	ndctl enable-region region0
	# BUILD DEVDAX ON /dev/dax0.0
	ndctl create-namespace --mode=dax -f

	# RECONFIG TO SYSTEM RAM
	daxctl reconfigure-device  -N --mode=system-ram dax0.0 --force

elif [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_3" ]; then
	echo "BiscuitOS CXL TYPE3: x1 Switch and x1 DDR"

	# CXL SETUP
	cxl destroy-region -f all
	cxl create-region -d decoder0.0 -m mem0 -s 512M -t ram -w 1 -g 4096 -u
	cxl enable-region region0

	# RECONFIG TO SYSTEM RAM
	daxctl reconfigure-device  -N --mode=system-ram dax0.0 --force

elif [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_4" ]; then
	echo "BiscuitOS CXL TYPE3: x3 Switch and x8 DDR"

	# CXL SETUP
	cxl destroy-region -f all
	cxl create-region -d decoder0.0 -m mem0 -s 256M -t ram -w 1 -g 4096 -u
	cxl enable-region region0

	# RECONFIG TO SYSTEM RAM
	daxctl reconfigure-device  -N --mode=system-ram dax0.0 --force

elif [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_5" ]; then
	echo "BiscuitOS CXL TYPE3: x3 Switch and x8 PMEM"

	# CXL SETUP
	cxl destroy-region -f all
	cxl create-region -d decoder0.0 -m mem0 -s 512M -t pmem -w 1 -g 4096 -u
	cxl enable-region region0

	# NDCTL LIST
	ndctl destroy-namespace --force all
	ndctl enable-region region0
	# BUILD DEVDAX ON /dev/dax0.0
	ndctl create-namespace --mode=dax -f

	# RECONFIG TO SYSTEM RAM
	daxctl reconfigure-device  -N --mode=system-ram dax0.0 --force

elif [ ${CXL_TOPOLOGY} = "BiscuitOS_CXL_TPG_6" ]; then
	echo "BiscuitOS CXL TYPE3: x3 Switch and x4 DDR/PMEM"

	echo "CXL VOLATILE MEMORY DOESN'T SUPPORT DEVDAX"
	exit 0

fi

# ENABLE DEMOTION
echo true > /sys/kernel/mm/numa/demotion_enabled
# ENABLE TIERED MEMORY ON NUMA BALANCING
echo 2 > /proc/sys/kernel/numa_balancing
# ENABLE DEMOTION ON RECLAIM
echo 7 > /proc/sys/vm/zone_reclaim_mode

BiscuitOS-CXL-TIERED-MEMORY-default &
